
package il.ac.tau.cvws.scrabblesidekick.client.ui;

import il.ac.tau.cvws.scrabblesidekick.client.app.AppServices;
import il.ac.tau.cvws.scrabblesidekick.client.app.controller.GameController;
import il.ac.tau.cvws.scrabblesidekick.client.app.controller.ScrabbleSidekickController;
import il.ac.tau.cvws.scrabblesidekick.client.ext.FacebookExtension;
import il.ac.tau.cvws.scrabblesidekick.client.ui.constant.UserMessageConstant;
import il.ac.tau.cvws.scrabblesidekick.client.ui.service.UIServices;
import il.ac.tau.cvws.scrabblesidekick.client.ui.view.RackView;
import il.ac.tau.cvws.scrabblesidekick.client.ui.view.camera.CameraState;
import il.ac.tau.cvws.scrabblesidekick.client.ui.view.solution.SolutionView;
import il.ac.tau.cvws.scrabblesidekick.client.util.dictionary.Dictionary;
import il.ac.tau.cvws.scrabblesidekick.shared.constant.BoardConstants;
import il.ac.tau.cvws.scrabblesidekick.shared.datatype.WordPlacement;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.View;

/* Main game activity. First, the user enters their rack, then a picture of
 * the board is taken and sent to the server for analysis.
 */
@SuppressLint("NewApi")
public class GameActivity extends CameraActivity
{
	private RackView  _rackView;
	private boolean   _isInitialized; 
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		init();
	}
	
	/* Display the solution in a special dialog.
	 */
	public void showSolution(WordPlacement p)
	{
		Dictionary dict = AppServices.instance().dictionary();
		
		final String word = dict.toDisplayableString(p.getWord());
		
		SolutionView solView = new SolutionView(this, new SolutionView.EventHandler()
		{
			@Override
			public void onFacebookClicked()
			{
				FacebookExtension.instance().postSolution(GameActivity.this, word, new FacebookExtension.Callback()
				{
					@Override
					public void onSuccess()
					{
						UIServices.showDialog(_context, "Facebook", UserMessageConstant.FACEBOOK_SUCCESS, null);
					}
					
					@Override
					public void onFailure()
					{
						UIServices.showDialog(_context, "Facebook", UserMessageConstant.FACEBOOK_FAILURE, null);
					}
					
					@Override
					public void onCancel()
					{
						// Do nothing
					}
				});
			}
		});
		
		for (int row = 0; row  < BoardConstants.BOARD_TILES_NUM; row++)
		{
			for (int col = 0; col < BoardConstants.BOARD_TILES_NUM; col++)
			{
				char c = dict.getChar(p.getLayout()[row][col]);
				
				solView.setLayoutLetter(c, row, col);
			}
		}
		
		int row = p.getStart().getRow();
		int col = p.getStart().getColumn();
		
		for (int i : p.getWord())
		{
			char c = dict.getChar(i);
			
			solView.setSolutionLetter(c, row, col);
			
			switch (p.getDirection())
			{
				case HORIZONTAL: col++; break;
				case VERTICAL:   row++; break;
				
				default: throw new IllegalStateException();
			}
		}
		
		UIServices.showViewBasedDialog(this, solView, new OnCancelListener()
		{
			@Override
			public void onCancel(DialogInterface dialog)
			{
				finish();
			}
		});
	}
	
	public void init()
	{
		if (!_isInitialized)
		{
			_isInitialized = true;
			
			boolean flag = ((GameController)_controller).start();
			
			if (!flag)
			{
				loadFailed();
			}
			else
			{
				buildCameraView();
				buildRackView();
				showRackLayout();
			}
		}		
	}
	
	private void buildRackView()
	{
		_rackView = new RackView((GameController)_controller, this);
		_layout.addView(_rackView);
	}
	
	private void loadFailed()
	{
		UIServices.showDialog(this, "", UserMessageConstant.CALIBRATION_IS_MISSING, new OnCancelListener()
		{
			@Override
			public void onCancel(DialogInterface dialog)
			{
				finish();				
			}
		});
		
	}
	
	public void showRackLayout()
	{	
		_state = CameraState.OFF;
		
		_rackView.setVisibility(View.VISIBLE);
		_cameraView.setVisibility(View.GONE);
		
		UIServices.showDialog(this, "", UserMessageConstant.RACK_INSTRUCTIONS,
							  new OnCancelListener()
							  {
								  @Override
								  public void onCancel(DialogInterface dialog)
								  {
									  _rackView.showKeyboard();				
								  }
							  });
	}
	
	public void showCameraLayout()
	{	
		_state = CameraState.ON;
		
		_rackView.setVisibility(View.GONE);
		_cameraView.setVisibility(View.VISIBLE);
		
		UIServices.showDialog(this, "", UserMessageConstant.LAYOUT_INSTRUCTIONS,
				  new OnCancelListener()
				  {
					  @Override
					  public void onCancel(DialogInterface dialog)
					  {
							_cameraView.restart();						  				
					  }
				  });
	}
	
	@Override
	protected ScrabbleSidekickController createController()
	{
		return new GameController(this);
	}

	@Override
	public void innerHandleSuccess()
	{
		_handler.hideProgressBar();
		
		WordPlacement placement = ((GameController)_controller).retrieveSoluion();
		showSolution(placement);
	}
}